﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：ProcureCatalogList.cs
    // 文件功能描述：产品库查询
    
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.DrugLibQuery
{
    public partial class UpdateProcureCatalogList : BasePage
    {
        public UpdateProcureCatalogList()
        {
            this.ModuleNo = Module.产品信息.ToString();
        }
    
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定所有项目
                //ProcurementProject procurementProject = new ProcurementProject();
                //procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                //procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                //procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                //procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                //int temp;
                //IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "PROJECTNAME";
                //ddlProject.DataValueField = "PROJECTID";
                //ddlProject.DataBind();
                //ListItem item = new ListItem();
                //item.Text = Constants.ALL_ITEM_TEXT;
                //item.Value = Constants.ALL_ITEM_VALUE;
                //ddlProject.Items.Insert(0, item);
                setBind();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 根据查询条件绑定采购目录
        /// </summary>
        private void setBind()
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyID_TB = CurrentAccount.GetUser().UserID;

            procureCatalog.PinyinCode = txtpinyincode.Text.Trim().ToUpper();
            procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.Material = txtMaterial.Text.Trim();
            //procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
            procureCatalog.GoodsID = txtGoodsid.Text.Trim();

            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.IsUsing = true;
            //procureCatalog.CompanyName_SC = txtCompany_SC.Text;
            //procureCatalog.SortName = txtProductType.Text;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();

            procureCatalog.Append = " nvl(SubmitStatus,0) " + (ddlSubmitStatus.SelectedItem.Value == "1" ? "=1" : ddlSubmitStatus.SelectedItem.Value == "0" ? "=0" : " in (0,1)") + " and nvl(CheckStatus,0) " + (ddlCheckstatus.SelectedItem.Value == "1" ? "=1" : ddlCheckstatus.SelectedItem.Value == "2" ? "=2" : ddlCheckstatus.SelectedItem.Value == "0" ? "=0" : " in (0,1,2)");

            procureCatalog.SortFields = " ProcureCataLogID";

         
            int totalCount = 0;
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            try
            {
                IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwProduceCatalog.DataSource = procureCatalogList;
                gvwProduceCatalog.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
        }
        }

        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
             
                //txtCompany_SC.Text = "";
                txtGoodsid.Text = "";
                this.txtpinyincode.Text = "";
                
                //txtProductType.Text = "";
                txtUnit.Text = "";
                txtMaterial.Text = "";
                txtGoodsName.Text = "";
                //ddlProject.SelectedIndex = -1;
            }
        }
        protected void gvwProduceCatalog_Command(object sender,GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                string id = e.CommandArgument.ToString();
                Response.Redirect("UpdateProcureCatalog.aspx?GoodsId=" + id);
            }
       
           
        }
        public string GetCheck(string flag)
        {
            if (flag == "1")
            {
                return "<font color=green>审核通过</font>";
            }
            if (flag == "2")
            {
                return "<font color=red>审核不通过</font>";
            }
            else
            {
                return "未审核";
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                CheckBox check = (CheckBox)gvwProduceCatalog.Rows[i].FindControl("check");
                if (check.Checked)
                {
                    string GoodsId = gvwProduceCatalog.DataKeys[i].Value.ToString();

                    flag += ipb.SubmitProcureCatalogList(GoodsId, 0,"");
                }
              
            }
            if (flag > 0)
            {
                HZ.Web.WebUtil.Alert("提交成功！");
                setBind();

            }
            else
            {
                HZ.Web.WebUtil.Alert("至少选择一项！");
                setBind();
            }
        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[6].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[6].Text = "采购限价";
                }
                gvwProduceCatalog.Columns[8].Visible = false;
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyID_TB = CurrentAccount.GetUser().UserID;

            procureCatalog.PinyinCode = txtpinyincode.Text.Trim().ToUpper();
            procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.Material = txtMaterial.Text.Trim();
            procureCatalog.GoodsID = txtGoodsid.Text.Trim();

            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.IsUsing = true;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();

            procureCatalog.Append = " nvl(SubmitStatus,0) " + (ddlSubmitStatus.SelectedItem.Value == "1" ? "=1" : ddlSubmitStatus.SelectedItem.Value == "0" ? "=0" : " in (0,1)") + " and nvl(CheckStatus,0) " + (ddlCheckstatus.SelectedItem.Value == "1" ? "=1" : ddlCheckstatus.SelectedItem.Value == "2" ? "=2" : ddlCheckstatus.SelectedItem.Value == "0" ? "=0" : " in (0,1,2)");

            procureCatalog.SortFields = " ProcureCataLogID";


            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.ProcureCatalog>(procureCatalog, true);
            string sql = string.Format(@"SELECT Goodsid 产品编号,
       GoodsName 产品名称,
       Outlookc 规格型号,
       Material 材质,
       Unit 最小计量单位,
       case ProjectID when '0002' then procureprice else bidprice end 价格,
       PackCount 最小零售包装数量,
       PackUnit 最小零售包装单位,
       QualityLevel 产品类别,
       CompanyName_SC 生产企业,
       CompanyName_TB 投标企业
  FROM View_ProcureCatalog_tb
 WHERE 1 = 1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "基础产品库.xls");
        }
    }
}
